package com.caifeng.contentproviderwork;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.icu.lang.UScript;
import android.net.Uri;

/**
 * 内容提供者
 */
public class PersonContentProvider extends ContentProvider {
    private static UriMatcher uriMatcher = new UriMatcher(-1);
    private static final int SUCCESS = 1;//匹配成功返回1
    private PersonSQLiteOpenHelper helper;//数据库对象

    //匹配Uri
    static {
        //写包名
        uriMatcher.addURI("com.caifeng.contentproviderwork", "info", SUCCESS);
    }

    public PersonContentProvider() {

    }

    //当创建内容提供者时调用
    @Override
    public boolean onCreate() {
        helper = new PersonSQLiteOpenHelper(getContext());
        return false;//
    }

    //查询数据库
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        int code = uriMatcher.match(uri);
        if (code == SUCCESS) {
            SQLiteDatabase db = helper.getReadableDatabase();
            return db.query("info", projection, selection, selectionArgs, null, null, sortOrder);
        } else {
            throw new IllegalArgumentException("路径非法");
        }
    }

    //暂时没什么用
    @Override
    public String getType(Uri uri) {

        return null;
    }

    //添加操作
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int code=uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getReadableDatabase();
            long rowID=db.insert("info",null,values);
            if(rowID>0){
                Uri uri1= ContentUris.withAppendedId(uri,rowID);
                getContext().getContentResolver().notifyChange(uri1,null);
                return uri1;
            }
            db.close();
            return uri;

        }else {
            throw new IllegalArgumentException("路径不对，无法插入数据");

        }
    }

    //删除数据
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int code=uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getWritableDatabase();
            int count=db.delete("info",selection,selectionArgs);
            if(count>0){
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else{
            throw new IllegalArgumentException("路径不对，无法删除数据");
        }

    }

    //数据更新
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int code=uriMatcher.match(uri);
        if(code==SUCCESS){
            SQLiteDatabase db=helper.getReadableDatabase();
            int count=db.update("info",values,selection,selectionArgs);
            if(count>0){
                getContext().getContentResolver().notifyChange(uri,null);
            }
            db.close();
            return count;
        }else {
            throw new IllegalArgumentException("路径不对，无法更行数据");
        }
    }
}
